package edu.bw.web.dept;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.pojo.Dept;
import edu.bw.service.DeptService;
import edu.bw.service.impl.DeptServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/selectById")
public class SelectByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        DeptService deptService = new DeptServiceImpl();
        //{"id":7}
        String reqJSON = request.getReader().readLine();
        Dept dept = JSONUtil.toBean(reqJSON, Dept.class);

        List<Map<String, Object>> list = deptService.selectById(dept.getId());

        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok(list.get(0))));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
